function updateTotal()
{
	var total = 0;
	var total_bill = 0;
	var sale_credit = document.getElementById('sale_credit').value;
	var sale_cash = document.getElementById('sale_cash').value;
	
	if( sale_cash == '' )
	{
		sale_cash = 0;	
	}
	
	if( sale_credit != '')
	{
		total += parseFloat(sale_credit);
	}
	if( sale_cash != '')
	{
		total += parseFloat(sale_cash);
	}
			
	bill_paid = document.getElementsByName('bill_paid[]');		
	for(i = 0; i < bill_paid.length; i++)
	{
		if( bill_paid[i].value != '')
		{
			total_bill += parseFloat(bill_paid[i].value);
		}
	}
	
	document.getElementById('total_bill_paid').value = total_bill;		
	document.getElementById('total_sale').value = total;
	
	document.getElementById('cash_left').value = sale_cash - total_bill;
	
	
}



function addBillRow(){
	
	var html = $('#hidden_bill_row').html();	
	$('#div_bill_paid').append(html);
	
}

function removeBillRow(obj){
	
	obj.parentNode.parentNode.removeChild(obj.parentNode);

}


function formValidate(action)
{
	var error_msg = '';
  	var sale_date = $('#sale_date').val();
	if(sale_date == '')
	{
	 error_msg += '<p>Please select date</p>';
	}
	 
	if(error_msg == '')
	{
		$('#main_error_div').html('');	
		$('#main_error_div').hide();
		return true;
	}
	else
	{
		$('#main_error_div').html(error_msg);
		$('#main_error_div').show();
		return false;
	}
}

// upload photo code

function confirmStatus(sale_id) //remove with ajax
{
	$.post(
				baseurl+"/admin/daily_sale.php", 
				{ action: "confirmStatus", sale_id : sale_id }, 
				function(data) 
				{
					if( data == 1 )
					{
						$('#confirm_'+sale_id).slideUp();
						$('#img_confirm_'+sale_id).slideDown();
						
						$('#edit_'+sale_id).slideUp();
						$('#img_edit_'+sale_id).slideDown();
						
						$('#delete_'+sale_id).slideUp();
						$('#img_delete_'+sale_id).slideDown();
					}
				} 
			);
	
	
}

function intOnly(myfield, e, dec)
{
	
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	e = e || window.event; 
	if (e.ctrlKey && (("vV").indexOf(keychar) > -1))
	{
		return true;
	}
	
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
	{   
		return true; 
	}
	else if (((".").indexOf(keychar) > -1))
	{
		if(myfield.value.indexOf('.') > -1)   
			return false;
		else
			return true;
	}	
	else if ((("0123456789.").indexOf(keychar) > -1))
	{
	   return true;
	}  
	
	return false;
	/*var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	e = e || window.event; 
	if (e.ctrlKey && (("vV").indexOf(keychar) > -1))
	{
		return true;
	}
	
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
	{   
		return true; 
	}

	else if ((("0123456789").indexOf(keychar) > -1))
	{
	   return true;
	}  
	
	return false;*/
}


function floatOnly(myfield, e, dec)
{
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	e = e || window.event; 
	if (e.ctrlKey && (("vV").indexOf(keychar) > -1))
	{
		return true;
	}
	
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
	{   
		return true; 
	}
	else if (((".").indexOf(keychar) > -1))
	{
		if(myfield.value.indexOf('.') > -1)   
			return false;
		else
			return true;
	}	
	else if ((("0123456789.").indexOf(keychar) > -1))
	{
	   return true;
	}  
	
	return false;
}

function checkVAT(obj, company_id)
{
	if( obj.checked ){
		$('#bill_paid_'+company_id).addClass('txtColor');		
		$('#vat_'+company_id).show();
	} else {
		$('#bill_paid_'+company_id).removeClass('txtColor');	
		$('#vat_'+company_id).hide();
	}
	
	
		
}
